<?php
/**
 * 项目日志
 * User: guojing
 * Date: 2020/4/13
 * Time: 18:16
 */

namespace app\api\model;

use think\Model;

class ProjectLog extends Model
{

    protected $name = 'project_log';

    public static function addRecord($addData)
    {
        $data = $addData;
        $data['createtime'] = time();
        $data['updatetime'] = time();
        return self::create($data);
    }

    /**
     * 获取列表
     */
    public static function dataList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['uid'])) {
            $where['uid'] = $params['uid'];
        }
        if (isset($params['project_id'])) {
            $where['project_id'] = $params['project_id'];
        }
        $list = self::where($where)->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                // 发布时间
                $listVal['publish_time'] = date('Y-m-d', $listVal['publish_time']);
                //工时
                $listVal['work_time'] = (float)(sprintf('%.2f',(($listVal['time_end'] - $listVal['time_start']) / 3600)));
                $listData[] = $listVal;
            }
        }
        return $listData;
    }

    public static function findInfoByCondition($condition = [], $isLock = 0)
    {
        if ($isLock == 1) {
            return self::where($condition)->lock(true)->find();
        } else {
            return self::where($condition)->find();
        }
    }

    public static function saveData($condition = [], $data = [])
    {
        if ($condition && $data) {
            $data['updatetime'] = time();
            return self::where($condition)->update($data);
        }
    }

    public static function bdataList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['project_id'])) {
            $where['pl.project_id'] = $params['project_id'];
        }
        $list = db('project_log')->alias('pl')->join('project_assign pa', 'pl.project_id = pa.project_id and pl.uid = pa.uid', 'inner')->where($where)->field('pl.*,pa.category_id')->order('pa.category_id,pl.id desc')->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                //用户
                $user = User::findInfoByCondition(['id' => $listVal['uid']]);
                $listVal['hua_name'] = $user['hua_name'];

                //岗位
                $category = Category::findInfoByCondition(['id' => $listVal['category_id']]);
                $listVal['category_name'] = $category ? $category['name'] : '';

                // 发布时间
                $listVal['publish_time'] = date('Y-m-d', $listVal['publish_time']);
                //工时
                $listVal['work_time'] = (float)(sprintf('%.2f',(($listVal['time_end'] - $listVal['time_start']) / 3600)));

                // 时段
                $listVal['time_period'] = date('H:i', $listVal['time_start']) . '-' . date('H:i', $listVal['time_end']);

                $listData[] = $listVal;

                unset($user);
                unset($category);
            }
        }
        return $listData;
    }

    public static function getProjectNewestLog($condition = [])
    {
        return self::where($condition)->order('time_end desc')->find();
    }

}